package com.mystnihon.freeremote.ui;

import android.annotation.TargetApi;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.MenuItem;
import com.mystnihon.freeremote.R;
import com.mystnihon.freeremote.ui.fragment.SettingsBoxFragment;
import com.mystnihon.freeremote.ui.fragment.SettingsImportFragment;

@TargetApi(15)
public class ICSSettingsActivity extends SherlockActivity implements OnCheckedChangeListener {
    private static final String CHECKED_RADIO_BUTTON_ID = "checkedRadioButtonId";

    private enum FragmentSettingType {
        Import, General

    }

    private RadioGroup mRGSettings;
    private View mContainer;
    private ActionBar actionBar;
    private boolean mIsPortrait = false;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);

        mRGSettings = (RadioGroup) findViewById(R.id.fr_setting_group);
        mContainer = findViewById(R.id.fr_setting_frame);

        mIsPortrait = getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
        
        actionBar = getSupportActionBar();
        actionBar.setHomeButtonEnabled(true);

        mRGSettings.setOnCheckedChangeListener(this);
        if (savedInstanceState != null) {
            mRGSettings.check(savedInstanceState.getInt(CHECKED_RADIO_BUTTON_ID));

        }
    }


    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt(CHECKED_RADIO_BUTTON_ID, mRGSettings.getCheckedRadioButtonId());
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.fr_setting_rb_general:
                switchFragment(FragmentSettingType.General);
                break;
            case R.id.fr_setting_rb_impexp:
                switchFragment(FragmentSettingType.Import);
                break;
            case RadioGroup.NO_ID:
                mContainer.setVisibility(View.GONE);
                if (mIsPortrait) {
                    mRGSettings.setVisibility(View.VISIBLE);

                }
                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (mContainer.getVisibility() == View.VISIBLE) {
                mRGSettings.clearCheck();
                return true;
            } else {
                return super.onKeyDown(keyCode, event);
            }
        } else {
            return super.onKeyDown(keyCode, event);
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            if (mContainer.getVisibility() == View.VISIBLE) {
                mRGSettings.clearCheck();
                actionBar.setDisplayHomeAsUpEnabled(false);
                return true;
            } else {
                return super.onOptionsItemSelected(item);
            }

        } else {
            return super.onOptionsItemSelected(item);
        }
    }

    private void switchFragment(FragmentSettingType type) {
        FragmentManager fm = getFragmentManager();
        Fragment frag = fm.findFragmentById(R.id.fr_setting_frame);

        switch (type) {
            case General:
                if (frag == null || !(frag instanceof SettingsBoxFragment)) {
                    FragmentTransaction ft = fm.beginTransaction();
                    SettingsBoxFragment fragment = new SettingsBoxFragment();
                    ft.replace(R.id.fr_setting_frame, fragment);
                    ft.commit();
                }
                break;
            case Import:
                if (frag == null || !(frag instanceof SettingsImportFragment)) {
                    FragmentTransaction ft = fm.beginTransaction();
                    SettingsImportFragment fragment = new SettingsImportFragment();
                    ft.replace(R.id.fr_setting_frame, fragment);
                    ft.commit();
                }
                break;
        }
        actionBar.setDisplayHomeAsUpEnabled(true);
        if (mContainer.getVisibility() != View.VISIBLE) {
            mContainer.setVisibility(View.VISIBLE);
            if (mIsPortrait) {
                mRGSettings.setVisibility(View.GONE);
            }
        }
    }
}
